﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using EnvDTE;

using VsWebSite;
using System.IO;
using EnvDTE90;

namespace WyoCode.WyoApp_VSTools
{
    /// <summary>
    /// Interaction logic for MyControl.xaml
    /// </summary>
    /// 


    public partial class MyControl : UserControl
    {

        List<VSProjectFile> ProjectList = new List<VSProjectFile>();

        public MyControl()
        {
            InitializeComponent();

            Refresh();

            ((SolutionEvents)WyoApp_VSToolsPackage._applicationObject.Solution.DTE.Events.SolutionEvents).Opened += new _dispSolutionEvents_OpenedEventHandler(MyControl_Opened);
            WyoApp_VSToolsPackage._applicationObject.Solution.DTE.Events.SolutionEvents.Opened += new _dispSolutionEvents_OpenedEventHandler(SolutionEvents_Opened);
            WyoApp_VSToolsPackage._applicationObject.Solution.DTE.Events.SolutionEvents.ProjectAdded += new _dispSolutionEvents_ProjectAddedEventHandler(SolutionEvents_ProjectAdded);
        }

        void MyControl_Opened()
        {
            Refresh();
        }

        void SolutionEvents_ProjectAdded(Project Project)
        {
            Refresh();
        }

        void SolutionEvents_Opened()
        {
            Refresh();
        }


        private void Refresh()
        {
            ProjectList = new List<VSProjectFile>();
            if (WyoApp_VSToolsPackage._applicationObject.Solution != null)
            {

                foreach (Project item in WyoApp_VSToolsPackage._applicationObject.Solution.Projects)
                {

                    if (File.Exists(item.FullName))
                    {
                        ProjectList.Add(new VSProjectFile(item));
                    }


                }
            }
            this.ProjectListBox.ItemsSource = ProjectList;
        }


        private void RefreshClick(object sender, RoutedEventArgs e)
        {
            Refresh();
        }

        private void ProjectListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.wcfRootControl.Project = (VSProjectFile)ProjectListBox.SelectedItem;
        }
    }
}